{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>GENDER</th>\n",
       "      <th>MARRIAGE</th>\n",
       "      <th>EDU_EXPERIENCE</th>\n",
       "      <th>WORK_SIZE</th>\n",
       "      <th>WORK_POWER</th>\n",
       "      <th>IS_ILLEGAL_HIS</th>\n",
       "      <th>CURR_FREEZE_VALUE</th>\n",
       "      <th>GRADUATE_YEAR</th>\n",
       "      <th>OCCUPATION</th>\n",
       "      <th>OCCUPATION_TYPE</th>\n",
       "      <th>VIP_FLAG</th>\n",
       "      <th>GRAY_FLAG</th>\n",
       "      <th>FIVE_CLASS_TYPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15735</th>\n",
       "      <td>51</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15741</th>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>60</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15753</th>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>70</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9</td>\n",
       "      <td>z</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15788</th>\n",
       "      <td>41</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>70</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>z</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15797</th>\n",
       "      <td>42</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AGE  GENDER  MARRIAGE  EDU_EXPERIENCE  WORK_SIZE  WORK_POWER  \\\n",
       "15735   51       1         2              99          2           1   \n",
       "15741   56       1         2              60          2           1   \n",
       "15753   45       1         2              70          2           1   \n",
       "15788   41       1         2              70          2           1   \n",
       "15797   42       1         3              70          3           1   \n",
       "\n",
       "       IS_ILLEGAL_HIS  CURR_FREEZE_VALUE  GRADUATE_YEAR  OCCUPATION  \\\n",
       "15735             2.0                0.0            4.0           9   \n",
       "15741             2.0                0.0            4.0           9   \n",
       "15753             2.0                0.0            3.0           9   \n",
       "15788             2.0                0.0            4.0           9   \n",
       "15797             2.0                0.0            4.0           9   \n",
       "\n",
       "      OCCUPATION_TYPE  VIP_FLAG  GRAY_FLAG  FIVE_CLASS_TYPE  \n",
       "15735               5         0          0                0  \n",
       "15741               5         0          0                0  \n",
       "15753               z         0          0                0  \n",
       "15788               z         0          0                0  \n",
       "15797               5         0          0                1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_path = './test2.csv'\n",
    "data = pd.read_csv(file_path,index_col=0)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((504, 10), (504,))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerical = ['AGE', 'WORK_SIZE', 'CURR_FREEZE_VALUE', 'GRADUATE_YEAR']\n",
    "\n",
    "categorical = ['EDU_EXPERIENCE', 'MARRIAGE', 'OCCUPATION', 'OCCUPATION_TYPE']\n",
    "\n",
    "binary = ['GENDER', 'WORK_POWER']\n",
    "\n",
    "train_X = data[numerical + categorical + binary]\n",
    "train_Y = data['FIVE_CLASS_TYPE']\n",
    "\n",
    "train_X.shape,train_Y.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字段|中文|类型\n",
    "--|--|--\n",
    "AGE|年龄|数值\n",
    "WORK_SIZE|劳动人口数|数值\n",
    "CURR_FREEZE_VALUE|账户冻结金额|数值\n",
    "GRADUATE_YEAR|工作年限|数值\n",
    "EDU_EXPERIENCE|最高学历|类别\n",
    "MARRIAGE|结婚|类别\n",
    "OCCUPATION|职务|类别\n",
    "OCCUPATION_TYPE|职业类型|类别\n",
    "GENDER|性别|二值\n",
    "WORK_POWER|劳动能力|二值\n",
    "IS_ILLEGAL_HIS|是否非法|删除\n",
    "VIP_FLAG|白名单客户|删除\n",
    "GRAY_FLAG|灰名单客户|删除\n",
    "FIVE_CLASS_TYPE|五级分类|目标值\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类别型变量进行One-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>EDU_EXPERIENCE_10</th>\n",
       "      <th>EDU_EXPERIENCE_20</th>\n",
       "      <th>EDU_EXPERIENCE_30</th>\n",
       "      <th>EDU_EXPERIENCE_40</th>\n",
       "      <th>EDU_EXPERIENCE_50</th>\n",
       "      <th>EDU_EXPERIENCE_60</th>\n",
       "      <th>EDU_EXPERIENCE_70</th>\n",
       "      <th>EDU_EXPERIENCE_80</th>\n",
       "      <th>EDU_EXPERIENCE_90</th>\n",
       "      <th>EDU_EXPERIENCE_99</th>\n",
       "      <th>...</th>\n",
       "      <th>OCCUPATION_4</th>\n",
       "      <th>OCCUPATION_9</th>\n",
       "      <th>OCCUPATION_TYPE_0</th>\n",
       "      <th>OCCUPATION_TYPE_1</th>\n",
       "      <th>OCCUPATION_TYPE_3</th>\n",
       "      <th>OCCUPATION_TYPE_4</th>\n",
       "      <th>OCCUPATION_TYPE_5</th>\n",
       "      <th>OCCUPATION_TYPE_6</th>\n",
       "      <th>OCCUPATION_TYPE_y</th>\n",
       "      <th>OCCUPATION_TYPE_z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15735</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15741</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15753</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15788</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15797</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       EDU_EXPERIENCE_10  EDU_EXPERIENCE_20  EDU_EXPERIENCE_30  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_40  EDU_EXPERIENCE_50  EDU_EXPERIENCE_60  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  1   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_70  EDU_EXPERIENCE_80  EDU_EXPERIENCE_90  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  1                  0                  0   \n",
       "15788                  1                  0                  0   \n",
       "15797                  1                  0                  0   \n",
       "\n",
       "       EDU_EXPERIENCE_99        ...          OCCUPATION_4  OCCUPATION_9  \\\n",
       "15735                  1        ...                     0             1   \n",
       "15741                  0        ...                     0             1   \n",
       "15753                  0        ...                     0             1   \n",
       "15788                  0        ...                     0             1   \n",
       "15797                  0        ...                     0             1   \n",
       "\n",
       "       OCCUPATION_TYPE_0  OCCUPATION_TYPE_1  OCCUPATION_TYPE_3  \\\n",
       "15735                  0                  0                  0   \n",
       "15741                  0                  0                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  0                  0   \n",
       "\n",
       "       OCCUPATION_TYPE_4  OCCUPATION_TYPE_5  OCCUPATION_TYPE_6  \\\n",
       "15735                  0                  1                  0   \n",
       "15741                  0                  1                  0   \n",
       "15753                  0                  0                  0   \n",
       "15788                  0                  0                  0   \n",
       "15797                  0                  1                  0   \n",
       "\n",
       "       OCCUPATION_TYPE_y  OCCUPATION_TYPE_z  \n",
       "15735                  0                  0  \n",
       "15741                  0                  0  \n",
       "15753                  0                  1  \n",
       "15788                  0                  1  \n",
       "15797                  0                  0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_dummies = pd.get_dummies(data[categorical],columns=categorical)\n",
    "data_dummies.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(504, 34)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X = pd.concat([data[numerical+binary],data_dummies],axis=1)\n",
    "train = pd.concat([train_X,train_Y],axis=1)\n",
    "train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取卡方值\n",
    "对年龄做探索性的分箱\n",
    "\n",
    "命中率，最理想的样本选择命中率是3：1~5：1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24404761904761904"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos_cnt = train_Y.sum()   # 命中率(坏人)\n",
    "all_cnt = train_Y.count() # 所有人\n",
    "expected_ratio = float(pos_cnt)/all_cnt\n",
    "expected_ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = 'AGE'\n",
    "target = 'FIVE_CLASS_TYPE'\n",
    "df = train[[col,target]]\n",
    "df=df.dropna()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count\n",
       "0   21      1\n",
       "1   22      1\n",
       "2   23      1\n",
       "3   25      1\n",
       "4   26      3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count = df[col].value_counts().sort_index().reset_index().rename(columns={\"index\":col,col:\"count\"})\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all\n",
       "0   21      1    0    1\n",
       "1   22      1    1    1\n",
       "2   23      1    0    1\n",
       "3   25      1    0    1\n",
       "4   26      3    0    3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count['hit']=df_count.apply(lambda a:train.loc[train[col]==a['AGE'],target].sum(),axis=1)\n",
    "df_count['all']=df_count.apply(lambda a:train.loc[train[col]==a['AGE'],target].count(),axis=1)\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.732143</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all  expected_cnt\n",
       "0   21      1    0    1      0.244048\n",
       "1   22      1    1    1      0.244048\n",
       "2   23      1    0    1      0.244048\n",
       "3   25      1    0    1      0.244048\n",
       "4   26      3    0    3      0.732143"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count['expected_cnt']=df_count['all']*expected_ratio\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGE</th>\n",
       "      <th>count</th>\n",
       "      <th>hit</th>\n",
       "      <th>all</th>\n",
       "      <th>expected_cnt</th>\n",
       "      <th>chi_sequare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>2.341609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.244048</td>\n",
       "      <td>0.244048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.732143</td>\n",
       "      <td>0.732143</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGE  count  hit  all  expected_cnt  chi_sequare\n",
       "0   21      1    0    1      0.244048     0.244048\n",
       "1   22      1    1    1      0.244048     2.341609\n",
       "2   23      1    0    1      0.244048     0.244048\n",
       "3   25      1    0    1      0.244048     0.244048\n",
       "4   26      3    0    3      0.732143     0.732143"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def chi_sequare_cal(hit,expected_count):\n",
    "    return (hit-expected_count)**2/expected_count\n",
    "df_count['chi_sequare']=df_count.apply(lambda row:chi_sequare_cal(row['hit'],row['expected_cnt']),axis=1)\n",
    "df_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.244048"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0-0.244048)**2/0.244048"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
